package models

import (
	"LoveU/utils"

	"github.com/astaxie/beego/orm"
)

// 获取最新的活动圈信息
func GetActivity() *ActivityReturn {
	o := orm.NewOrm()
	qs := o.QueryTable("activity")
	activity := new(Activity)
	err := qs.OrderBy("-data").One(activity)
	if err != nil {
		return nil
	}
	activityReturn := new(ActivityReturn)
	activityReturn.Id = activity.Id
	activityReturn.Content = activity.Content
	activityReturn.Data = activity.Data
	return activityReturn
}

//参加活动
func AttendActivity(aid int64, userid int64) int64 {
	o := orm.NewOrm()
	attendInfo := new(Attend)
	//是否重复参与活动
	err0 := o.QueryTable("attend").Filter("activityid", aid).Filter("userid", userid).One(attendInfo)
	if err0 == orm.ErrNoRows {
		//是否存在此ID对应的活动；被删除的活动视为“不存在”
		count, err1 := o.QueryTable("activity").Filter("id", aid).Exclude("delete", true).Count()
		if err1 != nil || count == 0 {
			return utils.ACTIVITY_NOT_FOUND
		}
		attend := &Attend{
			Activityid: aid,
			Userid:     userid,
		}
		_, err2 := o.Insert(attend)
		if err2 != nil {
			return utils.UNKNOWN_ERROR
		}
	} else if err0 == nil {
		return utils.ACTIVITY_ATTENDED
	} else {
		return utils.UNKNOWN_ERROR
	}
	return utils.ACTIVITY_ATTEND_OK
}

//获取全部的活动
func GetUserActivities(page, pagesize, userid int64) (*utils.Pagination, int64) {
	var activitiesReturn []*ActivityReturn

	o := orm.NewOrm()
	qs := o.QueryTable("activity").Exclude("delete", true).OrderBy("-data")
	activities := make([]*Activity, 0)
	startLine := (page - 1) * pagesize
	count, err0 := qs.Count()
	sum, err1 := qs.Limit(pagesize).Offset(startLine).All(&activities)
	if (err0 != nil && err0 != orm.ErrNoRows) || err1 != nil {
		return nil, utils.UNKNOWN_ERROR
	}

	for _, tmpActivity := range activities {
		acReturn := new(ActivityReturn)
		acReturn.Content = tmpActivity.Content
		acReturn.Data = tmpActivity.Data
		acReturn.Id = tmpActivity.Id
		status, _ := GetAttendStatus(tmpActivity.Id, userid)
		if status == utils.ACTIVITY_ATTENDED {
			acReturn.Attended = true
		} else {
			acReturn.Attended = false
		}
		activitiesReturn = append(activitiesReturn, acReturn)
	}
	var pagenum int64
	if count%pagesize == 0 {
		pagenum = count / pagesize
	} else {
		pagenum = count/pagesize + 1
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pagenum,
		Sum:       sum,
		Data:      activitiesReturn,
	}
	return data, utils.OK
}

// 返回某用户参加某活动的情况
func GetAttendStatus(activity, user int64) (int64, *Attend) {
	o := orm.NewOrm()
	attendRecord := new(Attend)
	err := o.QueryTable("attend").Filter("userid", user).Filter("activityid", activity).One(attendRecord)
	if err == orm.ErrNoRows {
		return utils.ACTIVITY_NOT_ATTENDED, nil
	} else if err == nil {
		return utils.ACTIVITY_ATTENDED, attendRecord
	} else {
		return utils.UNKNOWN_ERROR, nil
	}
}

//获取的我参加的活动
func GetMyActivities(userid, page, pagesize int64) (*utils.Pagination, int64) {
	var activitiesReturn []*ActivityReturn
	o := orm.NewOrm()
	qs := o.QueryTable("Attend").Filter("userid", userid).OrderBy("-data")
	count, err0 := qs.Filter("userid", userid).Count()
	attends := make([]*Attend, 0)
	startLine := (page - 1) * pagesize
	sum, err1 := qs.Limit(pagesize).Offset(startLine).All(&attends)
	if (err0 != nil && err0 != orm.ErrNoRows) || err1 != nil {
		return nil, utils.UNKNOWN_ERROR
	}

	var activityTmp *Activity
	for _, tmpAttend := range attends {
		activityTmp = getActivity(tmpAttend.Activityid)
		acReturn := new(ActivityReturn)
		acReturn.Content = activityTmp.Content
		acReturn.Data = activityTmp.Data
		acReturn.Id = activityTmp.Id
		activitiesReturn = append(activitiesReturn, acReturn)
	}

	var pagenum int64
	if count%pagesize == 0 {
		pagenum = count / pagesize
	} else {
		pagenum = count/pagesize + 1
	}
	data := &utils.Pagination{
		Page:      page,
		Pagesize:  pagesize,
		Pagecount: pagenum,
		Sum:       sum,
		Data:      activitiesReturn,
	}
	return data, utils.OK
}

//通过活动id获取活动对象
func getActivity(id int64) *Activity {
	o := orm.NewOrm()
	qs := o.QueryTable("activity")
	activity := new(Activity)
	err := qs.Filter("id", id).One(activity)
	if err != nil {
		return nil
	}
	return activity
}

//取消活动
func CancelActivity(activity, user int64) bool {
	o := orm.NewOrm()
	_, err := o.QueryTable("attend").Filter("userid", user).Filter("activityid", activity).Delete()
	if err == orm.ErrNoRows {
		return false
	} else if err == nil {
		return true
	} else {
		return false
	}
}
